package com.woniuxy.homepage.core.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CommonResult;
import com.woniuxy.homepage.core.mapper.CarGoodsMapper;
import com.woniuxy.homepage.core.model.CarGoods;
import com.woniuxy.homepage.core.service.CarGoodsService;
import com.woniuxy.homepage.core.vo.CarGoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/carGoods")
@Api(tags = "商品接口")
public class CarGoodsController {


    @Resource
    private CarGoodsMapper carGoodsMapper;

    @Resource
    private CarGoodsService carGoodsService;


    /**
     * Description : 根据id查询商品  <br/>
     * ChangeLog : 1. 创建 (2021/7/1 12:06 [Hello Mr. Guo]);
     * @param id
     * @return com.woniuxy.common.CommonResult<com.woniuxy.homepage.core.model.CarGoods>
    */
    @ApiOperation("根据id查询商品")
    @GetMapping("/selectById/{id}")
    public CommonResult<CarGoods> selectByID(@PathVariable("id") Integer id){
        CarGoods carGoods = this.carGoodsMapper.selectById(id);
        if (carGoods == null){
            return CommonResult.fail("查询商品失败");
        }
        return CommonResult.success(carGoods);
    }


    /**
     * Description : 查询所有轮胎  <br/>
     * ChangeLog : 1. 创建 (2021/7/1 15:34 [Hello Mr. Guo]);
     *
     * @return com.woniuxy.common.CommonResult<com.woniuxy.homepage.core.model.CarGoods>
    */
    @ApiOperation("查询所有轮胎")
    @GetMapping("/selectAllTyre")
    public  CommonResult<CarGoods> selectAllTyre(){
        List<CarGoods> list = this.carGoodsService.selectAllTyre();
        if (list == null){
            return CommonResult.fail("查询轮胎信息失败");
        }
        return CommonResult.success(list);
    }


    /**
     * Description : 查询所有音响设备  <br/>
     * ChangeLog : 1. 创建 (2021/7/1 16:14 [Hello Mr. Guo]);
     *
     * @return com.woniuxy.common.CommonResult<com.woniuxy.homepage.core.model.CarGoods>
    */
    @ApiOperation("查询所有音响")
    @GetMapping("/selectAllSound")
    public CommonResult<CarGoods> selectAllSound(){
        List<CarGoods> list = this.carGoodsService.selectAllSound();
        if (list == null){
            return CommonResult.fail("查询音响信息失败");
        }
        return CommonResult.success(list);
    }


    /**
     * Description : 精选推荐  <br/>
     * ChangeLog : 1. 创建 (2021/7/1 20:28 [Hello Mr. Guo]);
     *
     * @return com.woniuxy.common.CommonResult<com.woniuxy.homepage.core.model.CarGoods>
    */
    @ApiOperation("精选推荐")
    @GetMapping("/selectBoutique")
    public CommonResult<CarGoods> selectBoutique(){
        List<CarGoods> list = this.carGoodsService.selectBoutique();
        if (list == null){
            return CommonResult.fail("查询精选失败");
        }
        return CommonResult.success(list);
    }


    /**
     * Description : 平台端商品管理  <br/>
     * ChangeLog : 1. 创建 (2021/7/12 16:56 [Hello Mr. Guo]);
     * @param startpage
     * @param pagesize
     * @return com.woniuxy.common.CommonResult<com.github.pagehelper.PageInfo<com.woniuxy.homepage.core.model.CarGoods>>
    */
    @GetMapping("/selectGoodsManage")
    public CommonResult<PageInfo<CarGoods>> selectGoodsManage(int startpage , int pagesize){
        PageHelper.startPage(startpage, pagesize);
        Page<CarGoods> page = (Page<CarGoods>) this.carGoodsService.selectGoodsManage();
        PageInfo<CarGoods> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);
    }



    /**
     * Description : 推荐商品  <br/>
     * ChangeLog : 1. 创建 (2021/7/13 10:36 [Hello Mr. Guo]);
     * @param id
     * @return com.woniuxy.common.CommonResult
    */
    @GetMapping("/recommend/{id}")
    public CommonResult recommend(@PathVariable("id") Integer id){
        int i = this.carGoodsService.recommend(id);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }


    /**
     * Description : 取消推荐商品  <br/>
     * ChangeLog : 1. 创建 (2021/7/13 10:41 [Hello Mr. Guo]);
     * @param id
     * @return com.woniuxy.common.CommonResult
    */
    @GetMapping("disRecommend/{id}")
    public CommonResult disRecommned(@PathVariable("id") Integer id){
        int i = this.carGoodsService.disRecommned(id);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }



    /**
     * Description : 商品模糊查询  <br/>
     * ChangeLog : 1. 创建 (2021/7/13 12:13 [Hello Mr. Guo]);
     * @param carGoodsVo
     * @return com.woniuxy.common.CommonResult<com.github.pagehelper.PageInfo>
    */
    @PostMapping("selctGoods")
    public CommonResult<PageInfo> selectGoods(@RequestBody CarGoodsVo carGoodsVo){
        PageHelper.startPage(carGoodsVo.getPage(),carGoodsVo.getLimit());
        Page<CarGoods> page = (Page<CarGoods>) this.carGoodsService.selectGoods(carGoodsVo.getCarGoods());
        PageInfo<CarGoods> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);
    }



    /**
     * Description : 增加商品  <br/>
     * ChangeLog : 1. 创建 (2021/7/13 16:06 [Hello Mr. Guo]);
     * @param carGoods
     * @return com.woniuxy.common.CommonResult
    */
    @PostMapping("insertGoods")
    public CommonResult insertGoods(@RequestBody CarGoods carGoods){
        int i = this.carGoodsService.insertGoods(carGoods);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }



    /**
     * Description : 更改商品信息  <br/>
     * ChangeLog : 1. 创建 (2021/7/13 17:38 [Hello Mr. Guo]);
     * @param carGoods
     * @return com.woniuxy.common.CommonResult
    */

    @PostMapping("updateGoods")
    public CommonResult updateGoods(@RequestBody CarGoods carGoods){
        int i = this.carGoodsService.updateGoods(carGoods);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }



    /**
     * Description : 删除商品  <br/>
     * ChangeLog : 1. 创建 (2021/7/13 17:41 [Hello Mr. Guo]);
     * @param id
     * @return com.woniuxy.common.CommonResult
    */
    @GetMapping("deleteGoods/{id}")
    public CommonResult deleteGoods(@PathVariable("id") Integer id){
        int i = this.carGoodsService.deleteGoods(id);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }
}

